package com.goldsprite.ruletilemap.gdxexamples;

import com.goldsprite.ruletilemap.core.RuleTile;

import java.util.*;

public class RuleConfig {

	public static RuleTile createGrassRuleTile() {
		List<RuleTile.RuleEntry> ruleEntries = new ArrayList<>();

		// 定义所有ID变量
		String id_TopLeft = "1";
		String id_TopCenter = "2";
		String id_TopRight = "3";
		String id_MiddleLeft = "11";
		String id_Center = "12";
		String id_MiddleRight = "13";
		String id_BottomLeft = "21";
		String id_BottomCenter = "22";
		String id_BottomRight = "23";
		String id_InnerTopLeft = "7";
		String id_InnerTopRight = "15";
		String id_InnerBottomLeft = "6";
		String id_InnerBottomRight = "5";
		String id_InnerTopLeftAndBottomRight = "9";
		String id_InnerBottomLeftAndTopRight = "10";

		// 基础规则
		{
			// 左上角
			ruleEntries.add(new RuleTile.RuleEntry(id_TopLeft, "" +
				"???" +
				"?oy" +
				"?yy"));
			// 上中
			ruleEntries.add(new RuleTile.RuleEntry(id_TopCenter, "" +
				"???" +
				"yoy" +
				"yyy"));
			// 右上角
			ruleEntries.add(new RuleTile.RuleEntry(id_TopRight, "" +
				"???" +
				"yo?" +
				"yy?"));
			// 左中
			ruleEntries.add(new RuleTile.RuleEntry(id_MiddleLeft, "" +
				"?yy" +
				"?oy" +
				"?yy"));
			// 中心
			ruleEntries.add(new RuleTile.RuleEntry(id_Center, "" +
				"yyy" +
				"yoy" +
				"yyy"));
			// 右中
			ruleEntries.add(new RuleTile.RuleEntry(id_MiddleRight, "" +
				"yy?" +
				"yo?" +
				"yy?"));
			// 左下角
			ruleEntries.add(new RuleTile.RuleEntry(id_BottomLeft, "" +
				"?yy" +
				"?oy" +
				"???"));
			// 下中
			ruleEntries.add(new RuleTile.RuleEntry(id_BottomCenter, "" +
				"yyy" +
				"yoy" +
				"???"));
			// 右下角
			ruleEntries.add(new RuleTile.RuleEntry(id_BottomRight, "" +
				"yy?" +
				"yo?" +
				"???"));
		}

		// 高级规则(拐角)
		{
			// 左上内拐角
			ruleEntries.add(new RuleTile.RuleEntry(id_InnerTopLeft, "" +
				"nyy" +
				"yoy" +
				"yyy"));
			// 右上内拐角
			ruleEntries.add(new RuleTile.RuleEntry(id_InnerTopRight, "" +
				"yyn" +
				"yoy" +
				"yyy"));
			// 左下内拐角
			ruleEntries.add(new RuleTile.RuleEntry(id_InnerBottomLeft, "" +
				"yyy" +
				"yoy" +
				"nyy"));
			// 右下内拐角
			ruleEntries.add(new RuleTile.RuleEntry(id_InnerBottomRight, "" +
				"yyy" +
				"yoy" +
				"yyn"));

			// 左上右下内拐角
			ruleEntries.add(new RuleTile.RuleEntry(id_InnerTopLeftAndBottomRight, "" +
				"nyy" +
				"yoy" +
				"yyn"));

			// 左下右上内拐角
			ruleEntries.add(new RuleTile.RuleEntry(id_InnerBottomLeftAndTopRight, "" +
				"yyn" +
				"yoy" +
				"nyy"));
		}

		// 更新attentionList包含所有使用的ID
		String attentionList = buildAttentionList(ruleEntries);

		return new RuleTile("g1", ruleEntries, attentionList);
	}

	private static String buildAttentionList(List<RuleTile.RuleEntry> ruleEntries) {
		List<String> attentionArray = new ArrayList<>();
		attentionArray.add("g1"); // 添加组ID

		for (RuleTile.RuleEntry entry : ruleEntries) {
			String key = entry.getTargetId();
			if (!attentionArray.contains(key)) {
				attentionArray.add(key);
			}
		}

		return String.join(" ", attentionArray);
	}

    public static String createRandomMap(int width, int height, double grassDensity) {
        Random random = new Random();
        StringBuilder map = new StringBuilder();

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 边界总是空气
                if (x == 0 || x == width - 1 || y == 0 || y == height - 1) {
                    map.append("0");
                } else {
                    // 根据密度随机生成草地或空气
                    if (random.nextDouble() < grassDensity) {
                        map.append("g1");
                    } else {
                        map.append("0");
                    }
                }

                if (x < width - 1) {
                    map.append(" ");
                }
            }
            if (y < height - 1) {
                map.append("\n");
            }
        }

        return map.toString();
    }
}
